Skip to content

Restrict SpeciatedPopulation::species to enforce non-empty species invariant#149

Merged
HyperCodec merged 2 commits intodevfrom
copilot/sub-pr-148
Mar 10, 2026
Merged

Restrict SpeciatedPopulation::species to enforce non-empty species invariant#149
HyperCodec merged 2 commits intodevfrom
copilot/sub-pr-148

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

SpeciatedPopulation had fully public fields, allowing external construction with empty inner Vecs and causing panics in round_robin/round_robin_enumerate (index/modulo by zero).

Changes

  • speciation.rs — Make species field private (invariant: every inner Vec is non-empty). threshold stays pub. Add:
    • SpeciatedPopulation::new(threshold) — empty population constructor
    • fn species(&self) -> &[Vec<usize>] — read-only accessor
  • eliminator.rs / repopulator.rs — Migrate all population.species field accesses to population.species()
  • tests/speciation.rs — Replace struct literal construction with new() + insert_genome(); update all field accesses to the accessor

Before / After

// Before — could be constructed in an invalid state
let mut pop = SpeciatedPopulation {
    species: vec![vec![]],  // empty inner vec → panic in round_robin
    threshold: 0.5,
};

// After — only valid states reachable through the public API
let mut pop = SpeciatedPopulation::new(0.5);
pop.insert_genome(0, &genomes, &());  // always pushes vec![index]

let species: &[Vec<usize>] = pop.species();

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…rce invariants

Co-authored-by: HyperCodec <72839119+HyperCodec@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on restricting fields to enforce invariants Restrict SpeciatedPopulation::species to enforce non-empty species invariant Mar 10, 2026
@HyperCodec HyperCodec marked this pull request as ready for review March 10, 2026 13:26
Copy link
Owner

@HyperCodec HyperCodec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be better, but it's good enough (I doubt anyone will use this struct directly anyway)

@HyperCodec HyperCodec merged commit aeeee01 into dev Mar 10, 2026
4 checks passed
@HyperCodec HyperCodec deleted the copilot/sub-pr-148 branch March 10, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants